package com.okq.dao;

import com.okq.dao.base.Impl.BaseDao;
import com.okq.entity.Group;
import com.okq.util.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述 : 部门Dao
 * 作者 : zdl
 * 日期 : 2017/6/28 13:45
 */
@Repository
public class GroupDao extends BaseDao {

    public void saveGroup(Group group) {
        super.save(group);
    }

    public void deleteGroup(Group group) {
        super.delete(group);
    }

    public void updateGroup(Group group) {
        super.update(group);
    }

    public void saveOrUpdateGroup(Group group) {
        super.saveOrUpdate(group);
    }

    public Group getGroupById(String id) {
        return (Group) super.get(Group.class, id);
    }

    public List<Group> listAllGroup() {
        String hql = "FROM Group WHERE isDelete=0";
        return super.find(hql);
    }

    /**
     * 根据公司ID查询部门list
     *
     * @param comId 公司Id
     * @return 部门list
     */
    public List<Group> listGroupByComId(String comId) {
        String hql = "FROM Group WHERE isDelete = 0 AND companyId = '" + comId + "'";
        return super.find(hql);
    }

    /**
     * 分页查询部门信息
     *
     * @param page  分页对象
     * @param group 部门查询
     * @return page
     */
    public Page<Group> pageByGroup(Page<Group> page, Group group) {
        StringBuilder sb = new StringBuilder();
        sb.append("FROM Group g WHERE g.isDelete = 0");
        if (group != null) {
            Map<String, Object> param = new HashMap<>();
            String groupName = group.getGroupName();
            if (StringUtils.isNotBlank(groupName)) {
                sb.append(" AND g.groupName LIKE ?1 ");
                param.put("1", "%" + groupName + "%");
            }
            String companyId = group.getCompanyId();
            if (StringUtils.isNotBlank(companyId) && !"-1".equals(companyId)) {
                sb.append(" AND g.companyId = ?2 ");
                param.put("2", companyId);
            }
            sb.append(" ORDER BY g.createTime DESC ");
            return super.findByPage(page, sb.toString(), param);
        }
        return page;
    }

    /**
     * 根据部门名称和公司Id查询部门
     *
     * @param groupName 部门名称
     * @param companyId 公司Id
     * @return 部门
     */
    public Group getGroupByName(String groupName, String companyId) {
        String hql = "FROM Group WHERE isDelete = 0 AND groupName = ?1 AND companyId = ?2 ";
        Map<String, Object> param = new HashMap<>();
        param.put("1", groupName);
        param.put("2", companyId);
        return (Group) super.get(hql, param);
    }

}
